

package com.io.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.io.constant.Constant;
import com.io.dao.SysMenuDao;
import com.io.entity.SysMenuEntity;
import com.io.entity.SysRoleMenuEntity;
import com.io.service.SysMenuService;
import com.io.service.SysRoleMenuService;
import com.io.service.SysUserService;
import com.io.utils.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuDao sysMenuDao;

    @Override
    public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenuEntity> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenuEntity> userMenuList = new ArrayList<>();
        for (SysMenuEntity menu : menuList) {
            if (menuIdList.contains(menu.getId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenuEntity> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<SysMenuEntity> queryNotButtonList() {
        List<SysMenuEntity> sysMenuEntities = baseMapper.queryNotButtonList();
        return getSysMenuEntities(sysMenuEntities);
    }

    private List<SysMenuEntity> getSysMenuEntities(List<SysMenuEntity> sysMenuEntities) {
        Map<Long, SysMenuEntity> treeMap = new HashMap<>();
        for (SysMenuEntity sysMenuEntity : sysMenuEntities) {
            treeMap.put(sysMenuEntity.getId(), sysMenuEntity);
        }
        List<SysMenuEntity> rootTree = new ArrayList<>();
        for (Map.Entry<Long, SysMenuEntity> entry : treeMap.entrySet()) {
            if (entry.getValue().getParentId().compareTo(0L) == 0) {
                rootTree.add(entry.getValue());
            } else {
                // 父任务信息
                SysMenuEntity tasksP = treeMap.get(entry.getValue().getParentId());
                if (StringUtils.isEmpty(tasksP)) {
                    rootTree.add(entry.getValue());
                } else {
                    tasksP.getChildren().add(entry.getValue());
                }
            }
        }
        return rootTree;
    }

    @Override
    public List<SysMenuEntity> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public void delete(Long menuId) {
        //删除菜单
        this.removeById(menuId);
        //删除菜单与角色关联
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenuEntity>().eq("menu_id", menuId));
    }

    @Override
    public Set<String> findPermissionByRole(String role) {
        Set<String> permsList;
        // 系统管理员，拥有最高权限
        if (ShiroUtils.getUserId() == 1) {
            List<SysMenuEntity> menuList = sysMenuDao.selectByMap(new HashMap<>());
            permsList = new HashSet<>(menuList.size());
            for (SysMenuEntity menu : menuList) {
                permsList.add(menu.getPerms());
            }
        } else {
            permsList = sysMenuDao.queryAllPerms(ShiroUtils.getUserId());
        }
        return permsList;
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);
        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList) {
        List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

        for (SysMenuEntity entity : menuList) {
            // 目录
            if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {
                entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}
